package com.onlineclass.classserver.controller;

import com.onlineclass.classserver.constant.Restult;
import com.onlineclass.classserver.service.ICouseTeacherService;
import com.onlineclassbackstage.beans.Course;
import com.onlineclassbackstage.beans.ResultObject;
import com.onlineclassbackstage.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class CourseTeacherController {

    @Autowired
    ICouseTeacherService service;

    @Autowired
    private  RedisTemplate redisTemplate;


    private  User getUser(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String token = "";
        for (Cookie cookie : cookies) {
            if ("token".equals(cookie.getName())){
                token = cookie.getValue();
            }
        }
        return  (User) redisTemplate.opsForValue().get(token);
    }

    @RequestMapping(value = "/selectCoursesByTeacher")
    public ResultObject selectCoursesByTeacher(HttpServletRequest request){
        List<Course> courses = service.selectCoursesByTeacher(getUser(request));
        ResultObject resultObject = null;
        if (courses != null) {
            resultObject = new ResultObject(1,"查询完成",courses);
        }else {
            resultObject = new ResultObject(0,"查询失败","");
        }
        return resultObject;
    }

    @RequestMapping("/addCourseByTeacher")
    public ResultObject addCourseByTeacher(Course course,HttpServletRequest request){
        int result = service.insertCourseByTeacher(course, getUser(request));
        ResultObject resultObject = null;
        if (result == Restult.getRight()) {
            resultObject = new ResultObject(1,"插入完成","");
        }else {
            resultObject = new ResultObject(0,"插入失败","");
        }
        return resultObject;
    }







}
